import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/login',
    },
    {
      path: '/home',
      component: () => import('../pages/home/Index.vue'),
      children:[
        {
          path: '/home',
          redirect: '/home/index',
        },
        {
          path: 'index',
          component: () => import('../pages/home/index/IndexMain.vue'),
        },
        // 工作状态
        // 最新状态
        {
          path: 'working/new',
          component: () => import('../pages/home/workingstatus/New.vue'),
        },
        {
          path: 'working/hot',
          component: () => import('../pages/home/workingstatus/Hot.vue'),
        },
        {
          path: 'working/history',
          component: () => import('../pages/home/workingstatus/History.vue'),
        },
        
        {
          path: 'working/list',
          component: () => import('../pages/home/workingstatus/List.vue'),
        },
        // 执法日报
        {
          path: 'daily',
          component: () => import('../pages/home//daily/Index.vue'),
        },
        // 专项行动
            {
              path: 'action',
              component: () => import('../pages/home/special/action.vue'),
            },
            {
              path: 'report',
              component: () => import('../pages/home/special/report.vue'),
            },
            {
              path: 'statistics',
              component: () => import('../pages/home/special/statistics.vue'),
            },
            {
              path: 'upload',
              component: () => import('../pages/home/special/upload.vue'),
            },
        // 执法巡查
            {
              path: 'divisional',
              component: () => import('../pages/home/low/divisional.vue'),
            },
            {
              path: 'area',
              component: () => import('../pages/home/low/area.vue'),
            },
        // 动态巡查
        {
          path: 'dynamic',
          component: () => import('../pages/home/dynamic/Index.vue'),
        },
        // 自动排查
        {
          path: 'automatic',
          component: () => import('../pages/home/automatic/Index.vue'),
        },
        // 排班与出勤
        {
          path: 'attendance',
          component: () => import('../pages/home/attendance/Index.vue'),
        },
        // 个人中心
          {
            path: 'timeoff',
            component: () => import('../pages/home/personal/timeoff.vue'),
          },
          {
            path: 'set',
            component: () => import('../pages/home/personal/set.vue'),
          },
      ]
    },
    {
      path: '/login',
      component: () => import('../pages/login/Index.vue'),
    },
    {
      path: '/:pathMatch(.*)*',
      component: () => import('../pages/notfound/Index.vue'),
    },
  ],
})

//挂载路由导航守卫
router.beforeEach((to,from,next) => {
  //t0   将要访问的路径
  //from 代表从哪个路径跳转而来
  //next 是一个函数，表示放行
  if(to.path == '/login') return next();
  const tokenstr = window.sessionStorage.getItem('token');
  if(!tokenstr) return next('/login');  //如果token值存在则放行，不存在就跳转登录页面
  next();
})


export default router
